package com.shangjinrong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangjinrong.domain.Lend;
import com.shangjinrong.domain.LendItem;
import com.shangjinrong.domain.LendItemReturn;
import com.shangjinrong.domain.LendReturn;
import com.shangjinrong.mapper.LendItemMapper;
import com.shangjinrong.mapper.LendItemReturnMapper;
import com.shangjinrong.mapper.LendMapper;
import com.shangjinrong.mapper.LendReturnMapper;
import com.shangjinrong.service.LendItemReturnService;
import com.shangjinrong.service.UserBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Naruto
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    @Resource
    private LendReturnMapper lendReturnMapper;

    @Resource
    private LendMapper lendMapper;

    @Resource
    private LendItemMapper lendItemMapper;

    @Autowired
    private UserBindService userBindService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> addReturnDetail(Long lendReturnId) {

        //还款记录
        LendReturn lendReturn = lendReturnMapper.selectById(lendReturnId);
        //获取标的
        Lend lend = lendMapper.selectById(lendReturn.getLendId());
        //获取当前标的还款记录的当前期数的所有投资人回款信息集合
        List<LendItemReturn> lendItemReturnList = this.selectByLendReturnId(lendReturnId);

        List<Map<String, Object>> lendItemReturnDetailList = new ArrayList<>();

        lendItemReturnList.forEach(lendItemReturn -> {
            Map<String, Object> map = new HashMap<>();

            //获取投资记录
            Long lendItemId = lendItemReturn.getLendItemId();
            LendItem lendItem = lendItemMapper.selectById(lendItemId);

            //获取投资人id
            Long investUserId = lendItem.getInvestUserId();
            String bindCode = userBindService.getBindCodeByUserId(investUserId);


            //项目编号
            map.put("agentProjectCode", lend.getLendNo());
            //投资编号
            map.put("voteBillNo", lendItem.getLendItemNo());
            //投资人bindCode
            map.put("toBindCode", bindCode);
            map.put("transitAmt", lendItemReturn.getTotal());
            //本金
            map.put("baseAmt", lendItemReturn.getPrincipal());
            //利息
            map.put("benifitAmt", lendItemReturn.getInterest());
            //手续费，默认为0
            map.put("feeAmt", new BigDecimal(0));

            lendItemReturnDetailList.add(map);

        });

        return lendItemReturnDetailList;

    }

    //获取当前标的还款记录的当前期数的所有投资人回款信息集合
    private List<LendItemReturn> selectByLendReturnId(Long lendReturnId) {
        QueryWrapper<LendItemReturn> lendItemReturnQueryWrapper = new QueryWrapper<>();

        lendItemReturnQueryWrapper.eq("lend_return_id", lendReturnId);
        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(lendItemReturnQueryWrapper);

        return lendItemReturnList;
    }

    @Override
    public List<LendItemReturn> selectByLendId(Long lendId, Long userId) {
        QueryWrapper<LendItemReturn> lendItemReturnQueryWrapper = new QueryWrapper<>();

        lendItemReturnQueryWrapper
                .eq("lend_id", lendId)
                .eq("invest_user_id", userId);

        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(lendItemReturnQueryWrapper);

        return lendItemReturnList;

    }

    @Override
    public List<LendItemReturn> selectLendItemReturnList(Long id) {
        QueryWrapper<LendItemReturn> lendItemReturnQueryWrapper = new QueryWrapper<>();

        lendItemReturnQueryWrapper.eq("lend_return_id", id);

        List<LendItemReturn> lendItemReturnList = baseMapper.selectList(lendItemReturnQueryWrapper);

        return lendItemReturnList;
    }
}
